const Service = require("uni-cloud-router").Service;
module.exports = class projectAdminService extends Service{
	async getProjectInfo({userID,statusID}){
		let proInfo = await this.db.collection('project_store').where({
			pm_id:userID,
			project_status:statusID
		}).get();
		
		if(proInfo.affectedDocs !== 0 || proInfo.data.length !== 0){
			return {
				status:0,
				msg:"获取成功",
				data:proInfo.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async setProjectInfo(){
		let {
			pm_name,
			pm_post,
			pm_phone,
			project_name,
			project_address,
			hole_count,
			project_jia_info,
			salary_way,
			project_id,
			project_begin_date,
			project_end_date,
			drill_id,
			worker_ids,
			project_expend,
			project_income,
			pm_id,
			project_status,
			signIns
		} = this.ctx.data;
		
		let projectInfoInDB = await this.db.collection('project_store').where({
			project_id
		}).get();
		
		if(projectInfoInDB.affectedDocs !== 0 || projectInfoInDB.data.length !== 0){
			let editProjectInfo = await this.db.collection('project_store').where({
				drill_id
			}).update({
				pm_name,
				pm_post,
				pm_phone,
				project_name,
				project_address,
				hole_count,
				project_jia_info,
				salary_way,
				project_id,
				project_begin_date,
				project_end_date,
				drill_id,
				worker_ids,
				project_expend,
				project_income,
				project_status
			})
			
			if(editProjectInfo.affectedDocs !== 0){
				return {
					status: 0,
					mag: "保存成功"
				}
			}
		} else {
			let addProjectInfo = await this.db.collection('project_store').add(this.ctx.data)
			
			if (addProjectInfo.affectedDocs !== 0) {
				return {
					status: 0,
					mag: "保存成功"
				}
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getProjectDetail({id}){
		let proDetail = await this.db.collection('project_store').doc(id).get();
		
		if(proDetail.affectedDocs !== 0 || proDetail.data.length !== 0){
			return {
				status:0,
				msg:"成功",
				data:proDetail.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async updateProjectSalaryWay({type,id,salary_way}){
		const dbCmd = this.db.command;
		let updateRes;
		if(type === 'project'){
			//修改项目结算方式默认更新下面所有员工的结算方式
			let curProjectData = await this.db.collection('project_store').where({
				_id:id
			}).get();
			
			let project = curProjectData.data[0];
			
			let updateWK = project.worker_ids.filter(item => {
				item.salaryWay = salary_way;
				return item;
			})
			
			updateRes = await this.db.collection('project_store').where({
				_id:id
			}).update({
				salary_way:salary_way,
				worker_ids:updateWK
			})
			
		} else if(type === 'worker'){
			let event = this.ctx.data;
			updateRes = await this.db.collection('project_store').where({
				_id:id,
				'worker_ids.wkID':event.wkID
			}).update({
				'worker_ids.$.salaryWay':salary_way
			})
		}
		
		if(updateRes.affectedDocs !== 0){
			return {
				status:0,
				msg:"修改成功"
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async updateProjectStatus({id,proStatus,projectDate}){
		let updateProjectOption = {
			project_status: proStatus
		};
		
		proStatus === 2 ? updateProjectOption.project_begin_date = projectDate : updateProjectOption.project_end_date =
			projectDate
		let updateProjectRes = await this.db.collection('project_store').where({
			_id: id
		}).update(updateProjectOption)
		
		if (updateProjectRes.affectedDocs == 1) {
			return {
				status: 0,
				msg: "修改成功"
			}
		} else {
			return {
				status: 1,
				msg: "修改失败"
			}
		}
		
		return {
			status: -1,
			msg: "稍后重试"
		}
	}
	
	async updateSalaryStandard({form,projectID}){
		let editRes = await this.db.collection('project_store').where({
			_id:projectID
		}).update({	
			salary_standard_chi:form.salary_standard_chi,
			salary_standard_day:form.salary_standard_day
		})
		
		if(editRes.affectedDocs !== 0){
			return{
				status:0,
				msg:"成功"
			}
		} else {
			return{
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async updateHoleStatus({projectID,hole,status}){
		let editRes = await this.db.collection('project_store').where({
			_id:projectID,
			"holes.num":hole.num
		}).update({	
			'holes.$.status': status
		})
		
		if(editRes.affectedDocs !== 0){
			return{
				status:0,
				msg:"成功"
			}
		} else {
			return{
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getProjectWKID({projectID}){
		let checkTokenRes,
			workerIDs;
		const dbCmd = this.db.command;
		
		workerIDs = await this.db.collection("project_store")
		.field({'worker_ids':true})
		.where({_id:dbCmd.eq(projectID)})
		.get()
		
		if(workerIDs.affectedDocs !== 0 || workerIDs.data.length !== 0){
			return{
				status:0,
				msg:"获取成功",
				data:workerIDs.data
			}
		} else{
			return {
				status:1,
				msg:"没有员工"
			};
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getWorkerInfo({wkID}){
		let workerInfoRes = await this.db.collection('userdata').where({
			wkID
		}).get();
		
		if(workerInfoRes.affectedDocs !== 0 || workerInfoRes.data.length !== 0){
			return {
				status:0,
				msg:"成功",
				data:workerInfoRes.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getProjectExpend({projectID,pageNo,pageSize}){
		let projectExpendRes = await this.db.collection('project_expend')
		.aggregate()
		.match({
			projectID
		})
		.skip((pageNo-1)*pageSize)
		.limit(pageSize)
		.end()
		
		if(projectExpendRes.affectedDocs !== 0 || projectExpendRes.data.length !== 0){
			return{
				status:0,
				msg:"成功",
				data:projectExpendRes.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getProjectIncome({projectID,pageNo,pageSize}){
		let projectIncomeRes = await this.db.collection('project_income')
		.aggregate()
		.match({
			projectID
		})
		.skip((pageNo-1)*pageSize)
		.limit(pageSize)
		.end()
		
		if(projectIncomeRes.affectedDocs !== 0 || projectIncomeRes.data.length !== 0){
			return{
				status:0,
				msg:"成功",
				data:projectIncomeRes.data
			}
		} else {
			return {
				status:1,
				msg:"没有数据"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async wkGetWorkerInfo({projectID,wkID,pageNo,pageSize}){
		let res = await this.db.collection('signin_store')
		.aggregate()
		.match({
			projectID,
			wkID
		})
		.skip((pageNo-1)*pageSize)
		.limit(pageSize)
		.end()
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			return {
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async getWKhistoryProject({id,type}){
		/**
		 * "onProjectID":根据项目ID获取
		 * "onWKid":根据员工ID获取
		 */
		let event = this.ctx.data;
		let querys = {};
		type == 'onWKid' ? querys = {wkID:event.wkID} : querys = {projectID:id};
		let res = await this.db.collection('wk_history_project').where(querys).get();
		
		if(res.affectedDocs !== 0 || res.data.length !== 0){
			return{
				status:0,
				msg:"成功",
				data:res.data
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		//返回数据给客户端
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async uploadIncome({data,type}){
		let uploadIncomedRes;
		const event = this.ctx.data;
		if(type == 'add'){
			uploadIncomedRes = await this.db.collection('project_income').add(data);
		} else if(type == 'update'){
			uploadIncomedRes = await this.db.collection('project_income').where({
				_id:event._id
			}).update({
				income:data.income,
				vouchers:data.vouchers,
				revenue_company:data.revenue_company,
				revenue_type:data.revenue_type,
				date:data.date,
				check_status:data.check_status,
				projectID:data.projectID,
			});
		}
		
		if(uploadIncomedRes.affectedDocs !== 0){
			return {
				status:0,
				msg:"上传成功"
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async uploadExpend({data}){
		let uploadExpendRes = await this.db.collection('project_expend').add(data);
		if(uploadExpendRes.affectedDocs !== 0){
			return {
				status:0,
				msg:"上传成功"
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async deleteProject(_id){
		let deleteProject = await this.db.collection('project_store').doc(_id).remove();
		if(deleteProject.affectedDocs !== 0){
			return {
				status:0,
				msg:"删除成功"
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
	
	async editProject(data,_id){
		let editProject = await this.db.collection('project_store').doc(_id).update(data);
		if(editProject.affectedDocs !== 0){
			return {
				status:0,
				msg:"修改成功"
			}
		} else {
			return {
				status:1,
				msg:"稍后重试"
			}
		}
		
		return {
			status:-1,
			msg:"稍后重试"
		}
	}
}